/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.RegisterationBean;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.logic.db.CommonWSFunctionality;
import model.logic.db.UserFunctions;
import model.logic.nondb.CommonMethods;
import model.pojo.Settings;
import model.pojo.Users;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Maha
 */
public class RegisterationAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession(true);
        RegisterationBean myBean = ((RegisterationBean) form);
        UserFunctions uf = new UserFunctions();
        int checkEmail = uf.checkEmail(myBean.getEmail());
        if (checkEmail == 1) {
            ((RegisterationBean) form).setMsg("Email Exists, If You Forgot your password, please go to login and get it back");

        } else {
            Users user = new Users();
            user.setAnswer(myBean.getAnswer());
            user.setCreationDate(new Date());
            user.setQuestion(myBean.getSecretQuestion());
            user.setUserEmail(myBean.getEmail());
            user.setUserName(myBean.getUserName());
            user.setUserPassword(myBean.getPassword());
            user.setUserType(myBean.getUserType());
            user.setVerificationCode("kRTfSaEW");
            int register = uf.register(user);

            if (register == 1) {

                String body = "<html><body><center><h2>You are a member!</h2></center><p>Dear &nbsp;" + user.getUserName() + "<br/> you are receiving this mail "
                        + "because you registered in our website."
                        + "<br/><b>Your Mail:</b>" + user.getUserEmail() + "<br/><b>Password:</b>" + user.getUserPassword() + "<br/> </p><p>Now you can enjoy our services and be updated with out latest job vacancies</p>"
                        + "<br/>Start now, visit us at <a href=\"http://www.globisoft.com/Globisoft/HomePage\">www.globisoft.com</a>"
                        + "&nbsp;&nbsp;Sincsrely<br/>Globisoft Team<br/></body></html>";

                CommonWSFunctionality cf = new CommonWSFunctionality();
                Settings settings = cf.getSettings();
                CommonMethods.sendMail(settings.getSmtpUser(), settings.getSmtpPassword(), settings.getSmtpName(), settings.getRegisterationEmails(), user.getUserEmail(), "Welcome " + user.getUserName() + " to Globisoft Company Website", body);


                ((RegisterationBean) form).setMsg("Thanks for Registeration, you will be redirected after 10 seconds to HomePage");
                session.setAttribute("user", user);

            } else {
                ((RegisterationBean) form).setMsg("Can't Register at a moment");
            }
        }
        return mapping.findForward("op");
    }
}
